package com.jxs.android.weiget;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.jxs.android.ges.R;
import com.jxs.android.ges.data.A_Case_ProtoclInfo;
import com.jxs.android.util.StringUtils;

public class Bu_A_Case_Protocl_PageView extends LinearLayout {
	static public interface Protocl_Loading_Listener {
		public void p_loadingData(int currentPage);
	}

	private Context context;
	private Bu_Protocl_Item protocl_item;
	private TextView null_has_data;
	private ListView listView;
	private int currentPage = 1;
	private VisitRecordAdapter adapter;

	private View loadMoreView;
	private Button loadMoreButton;
	private Protocl_Loading_Listener dataListener;

	public Bu_A_Case_Protocl_PageView(Context context) {
		super(context);
		this.context = context;
		initView();
	}

	public void initTitle(String i_index, String i_agreement_name, String i_create_date, String i_end_date, String i_detain_type_name) {
		protocl_item.setValue(i_index, i_agreement_name, i_create_date, i_end_date, i_detain_type_name);
	}

	private void initView() {
		LayoutInflater.from(context).inflate(R.layout.bu_a_case_result_protocl_item, this);
		null_has_data = (TextView) findViewById(R.id.null_has_data);

		protocl_item = (Bu_Protocl_Item) findViewById(R.id.protocl_item);
		protocl_item.setBackgroud(R.drawable.row0);
		protocl_item.setTextColor(context.getResources().getColor(R.color.white));
		protocl_item.setagreement_nameGravityCenter();

		loadMoreView = LayoutInflater.from(context).inflate(R.layout.bu_loading_btn, null);
		loadMoreButton = (Button) loadMoreView.findViewById(R.id.loadMoreButton);
		loadMoreButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				loadMoreButton.setText(R.string.prompt_loading); //
				if (null != dataListener) {
					dataListener.p_loadingData(currentPage);
				}
			}
		});

		listView = (ListView) findViewById(R.id.common_listview);
		listView.addFooterView(loadMoreView);
		adapter = new VisitRecordAdapter();
		listView.setAdapter(adapter);

	}

	public void setDataListener(Protocl_Loading_Listener dataListener) {
		this.dataListener = dataListener;
	}

	public VisitRecordAdapter getAdapter() {
		return adapter;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public void setEmpty(String text) {
		null_has_data.setText(text);
		null_has_data.setVisibility(View.VISIBLE);
	}

	public void setEmptyVisibility(int visibility) {
		null_has_data.setVisibility(visibility);
	}

	public void setValue(String i_index, String i_agreement_name, String i_create_date, String i_end_date, String i_detain_type_name) {
		protocl_item.setValue(i_index, i_agreement_name, i_create_date, i_end_date, i_detain_type_name);
	}

	public void setA_Case_ProtoclInfos(List<A_Case_ProtoclInfo> custInfos) {
		adapter.setA_Case_ProtoclInfos(custInfos);
		adapter.notifyDataSetChanged();
	}

	private class VisitRecordAdapter extends BaseAdapter {

		List<A_Case_ProtoclInfo> workNoteInfos = new ArrayList<A_Case_ProtoclInfo>();

		public void setA_Case_ProtoclInfos(List<A_Case_ProtoclInfo> custInfos) {
			if (currentPage == 1) {
				this.workNoteInfos.clear();
			}
			this.workNoteInfos.addAll(custInfos);
		}

		@Override
		public int getCount() {
			return workNoteInfos.size();
		}

		@Override
		public A_Case_ProtoclInfo getItem(int position) {
			return workNoteInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			A_Case_ProtoclInfo custInfo = workNoteInfos.get(position);
			Bu_Protocl_Item bu_Protocl_Item = new Bu_Protocl_Item(context);
			String index = Integer.valueOf(position + 1) + "";
			if (StringUtils.isEmpety(custInfo.toString())) {
				index = "";
			}
			bu_Protocl_Item.setValue(index, custInfo.agreement_name, custInfo.create_date, custInfo.end_date, custInfo.detain_type_name);
			if (position % 2 == 0) {
				bu_Protocl_Item.setBackgroud(R.drawable.table_row0);
			} else {
				bu_Protocl_Item.setBackgroud(R.drawable.table_row1);
			}
			return bu_Protocl_Item;
		}
	}

	public View getLoadMoreView() {
		return loadMoreView;
	}

	public Button getLoadMoreButton() {
		return loadMoreButton;
	}

}
